package com.diploma.project.aiqiyi;

import com.diploma.project.aiqiyi.common.enums.CommonOutputEnum;

import java.io.Serializable;

/**
 * 成功 
 * 	200
 * 	{
 *    code: 0,
 *    message: '', // 可选
 *    data: {list: [], count: 10} // 可选
 *  }
 * 失败
 *  非200
 *  {
 *    code: 4位(区别于 HTTP Status 的 3 位数字)数字错误码,
 *    message: '错误原因',
 *    data: {} // 可选
 *  }
 * @author wenmingwang
 *
 */
public class CommonResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;
	
	private Integer code;
	private String message;
    private T data;

	public CommonResponse() {
		
	}

	public CommonResponse(Integer code, String message, T data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public static <T> CommonResponse<T> ok(String message, T data) {
		CommonResponse<T> response = new CommonResponse<T>();
		response.code = CommonOutputEnum.SUCCESS.getCode();
		response.message = message;
		response.data = data;

		return response;
	}

	public static <T> CommonResponse<T> success(T data) {
		return ok("success", data);
	}

	public static <T> CommonResponse<T> ok(String message) {
		return ok(message, null);
	}

	public static <T> CommonResponse<T> ok() {
		return ok(null);
	}

	public static <T> CommonResponse<T> fail(Integer code, String message, T data) {
		CommonResponse<T> response = new CommonResponse<T>();
		response.code = code;
		response.message = message;
		response.data = data;
		
		return response;
	}

	public static <T> CommonResponse<T> fail(CommonOutputEnum commonOutputEnum, Object... replaceMsg) {
		CommonResponse<T> response = new CommonResponse<T>();
		response.code = commonOutputEnum.getCode();
		response.message = String.format(commonOutputEnum.getMsg(), replaceMsg);

		return response;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
}
